package Dao.daoImpl;

import Dao.empDao;
import Model.Emp1;
import org.junit.Test;

import java.sql.*;

public class EmpDaoImpl implements empDao {
    static Connection preconn = null;
    PreparedStatement preparedStatement = null;


    static {
        try {
            preconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/2411", "root", "123456");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    //删除
    @Test
    public void delEmpDao(int empId) throws SQLException {
        String sql = "delete from Emp1 where empId=?";
        preparedStatement = preconn.prepareStatement(sql);
        preparedStatement.setInt(1, empId);

        int i = preparedStatement.executeUpdate();
        System.out.println("i = " + i);
//        close();
    }

    //查询
    /*
       @param emp
          @param len
     */

    @Test
    public void showEmpDao() throws SQLException {
        String sql = "select * from Emp1";
        preparedStatement = preconn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        Emp1 emp1 = new Emp1();
        while (resultSet.next()) {
            Integer empId = resultSet.getInt("empId");
            String name = resultSet.getString("name");
            String tel = resultSet.getString("tel");
            String dress = resultSet.getString("dress");
            Integer money = resultSet.getInt("money");
            String sta = resultSet.getString("sta");

            System.out.println("empId = " + empId);
            System.out.println("name = " + name);
            System.out.println("tel = " + tel);
            System.out.println("dress = " + dress);
            System.out.println("money = " + money);
            System.out.println("sta = " + sta);

            emp1.setEmpId(empId);
            emp1.setName(name);
            emp1.setTel(tel);
            emp1.setDress(dress);
            emp1.setMoney(money);
            emp1.setSta(sta);
        }
    }

    //修改
    @Test
    public boolean updateEmpDao(Emp1 obj, int empId1) throws SQLException {
        String sql = "update Emp1 set name=?,tel=?,dress=?,money=?,sta=? where empId=?";
        preparedStatement = preconn.prepareStatement(sql);

        preparedStatement.setString(1, obj.getName());
        preparedStatement.setString(2, obj.getTel());
        preparedStatement.setString(3, obj.getDress());
        preparedStatement.setInt(4, obj.getMoney());
        preparedStatement.setString(5, obj.getSta());
        preparedStatement.setInt(6, empId1);
        int i = preparedStatement.executeUpdate();
        System.out.println("i = " + i);
        return true;
    }

    /*增加
              数据访问层 添加员工信息
              @param emp Array 数据集合 地址
              @param user 一条员工信息
           */
    @Test
    public void addEmpDao(Emp1 obj) throws SQLException {
        String sql = "insert into Emp1 values (null,?,?,?,?,?)";
        preparedStatement = preconn.prepareStatement(sql);

        preparedStatement.setString(1, obj.getName());
        preparedStatement.setString(2, obj.getTel());
        preparedStatement.setString(3, obj.getDress());
        preparedStatement.setInt(4, obj.getMoney());
        preparedStatement.setString(5, obj.getSta());

        int i = preparedStatement.executeUpdate();
        System.out.println(i);
//        close();

    }

    /**
     * 根据ID查询一条数据
     *
     * @throws SQLException
     */
    public Emp1 findOneDataDao(int id) throws SQLException {
        String sql = "select * from Emp1 where empId=? limit 1";
        preparedStatement = preconn.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        Emp1 emp1 = new Emp1();
        if (resultSet.next()) {
            Integer empId = resultSet.getInt("empId");
            String name = resultSet.getString("name");
            String tel = resultSet.getString("tel");
            String dress = resultSet.getString("dress");
            Integer money = resultSet.getInt("money");
            String sta = resultSet.getString("sta");
            emp1.setEmpId(empId);
            emp1.setName(name);
            emp1.setTel(tel);
            emp1.setDress(dress);
            emp1.setMoney(money);
            emp1.setSta(sta);
            return emp1;
        } else {
            return null;
        }
    }

    public void close() throws SQLException {
        preconn.close();
        preparedStatement.close();
    }
}
